package com.zx.idc.backend.gui.scheduler.ins.filtermonitorlog.job;


import com.zx.idc.backend.gui.scheduler.ins.filtermonitorlog.job.base.BaseFilterAndMonitorInstructionJob;
import com.zx.idc.common.constants.InsSourceConstants;
import com.zx.idc.common.exception.EncryptException;
import com.zx.idc.ds.ins.entity.InsMonitor;
import com.zx.idc.ds.ins.entity.InsMonitorRule;
import com.zx.idc.ds.ins.service.IInsMonitorRuleService;
import com.zx.idc.ds.ins.service.IInsMonitorService;
import com.zx.idc.internal.exception.SendMessageException;
import com.zx.idc.internal.ins.InstructionCallResult;
import com.zx.idc.internal.service.SMMSMonitorInstructionService;
import com.zx.idc.qc.constants.ScheduleJobConstants;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 删除监测指令规则job
 * <p>
 * 该指令在集群环境下，一次只能一个节点执行
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class DelMonitorInstructionJob extends BaseFilterAndMonitorInstructionJob implements Job {

    private static final Logger LOG = LoggerFactory.getLogger(DelMonitorInstructionJob.class);

    @Autowired
    private SMMSMonitorInstructionService smmsMonitorInstructionService;

    @Autowired
    private IInsMonitorService iInsMonitorService;

    @Autowired
    private IInsMonitorRuleService iInsMonitorRuleService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        String commandId = (String) jobDataMap.get(ScheduleJobConstants.INSTRUCTION_COMMAND_ID_KEY);
        InsMonitor insMonitor = (InsMonitor) jobDataMap.get(ScheduleJobConstants.INSTRUCTION_ENTITY);
        LOG.info("执行删除监测规则任务，命令ID: {}", commandId);

        if (insMonitor == null) {
            LOG.error("执行删除监测规则任务失败，命令id：{} 在数据库中不存在", commandId);
            return;
        }

        if (InsSourceConstants.SMMS.equals(insMonitor.getInsSource())) {
            iInsMonitorService.deleteById(insMonitor.getId());
        }
        List<InsMonitorRule> insMonitorRules = insMonitor.getInsMonitorRules();
        if (insMonitorRules != null) {
            iInsMonitorRuleService.deleteBatchIds(insMonitorRules.stream().map(InsMonitorRule::getId).collect(Collectors.toList()));
        }
        for (String houseId : insMonitor.getHouseId().split(",")) {
            try {
                InstructionCallResult result = smmsMonitorInstructionService.delRules(insMonitor, houseId);
                if (result != null && result.getCode() == 200) {
                    LOG.info("执行删除监测规则任务成功，命令id：{}", commandId);
                    continue;
                }
            } catch (EncryptException e) {
                LOG.error("删除监测规则任务执行失败，原因：保存的过程中，数据加密报错。具体错误：" + e.getMessage(), e);
            } catch (SendMessageException e) {
                LOG.error("删除监测规则任务执行失败。具体错误：" + e.getMessage(), e);
            }
            addTLogAlarm("删除监测规则任务", "删除监测规则任务执行失败");
        }
    }

}

